package com.qianxx.component.tool;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;

/**
 * 分割线样式
 */

public class MyItemDecoration extends RecyclerView.ItemDecoration {
    private int space;

    public MyItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
        if (parent.getChildPosition(view) == 0) {
            outRect.top = 0;
        } else {
            outRect.top = space;
        }
        if(parent.getChildPosition(view) == parent.getAdapter().getItemCount()-1){
            outRect.bottom = 0;
        }else{
            outRect.bottom = space;
        }
        outRect.left = space;
        outRect.right = space;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        // 初始化画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.parseColor("#dfdfdf"));
        // 开始画
        for (int i = 0, size = parent.getChildCount(); i < size; i++) {
            View view = parent.getChildAt(i);
            c.drawLine(view.getLeft(),view.getBottom(),
                    ((WindowManager) parent.getContext().getSystemService(
                            Context.WINDOW_SERVICE)).getDefaultDisplay()
                            .getWidth(), view.getBottom(), paint);
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

    }
}